package com.amado.course45mysql03.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.amado.course45mysql03.domain.Role;
import com.amado.course45mysql03.domain.RoleOptMapping;
import com.amado.course45mysql03.service.RoleOptMappingService;
import com.amado.course45mysql03.service.RoleService;
import com.amado.course45mysql03.service.impl.RoleOptMappingServiceImpl;
import com.amado.course45mysql03.service.impl.RoleServiceImpl;
import com.amado.course45mysql03.vo.Page;

public class RoleContorl {
	private RoleService roleService = new RoleServiceImpl();
	private RoleOptMappingService roleOptService = new RoleOptMappingServiceImpl();
	public static void main(String[] args) {
		RoleContorl roleContorl = new RoleContorl();
		roleContorl.bandle();
		// roleContorl.listPage();
	}

	private void bandle() {
		System.out.println("输入要绑定的角色");
		Scanner scanner = new Scanner(System.in);
				
		String roleCode = scanner.next();
		System.out.println("输入要绑定的操作权限,逗号分隔");
		// "adduser,TR,@all";
		String optOne = scanner.next();
		//TR,@ALL,JY,OTHER  
		String array[] = optOne.split(",");
		List<RoleOptMapping> optList = new ArrayList<>();
		for (String str : array) {
			RoleOptMapping roleOpt = new RoleOptMapping();
			roleOpt.setRoleCode(roleCode);//admin
			roleOpt.setOperationCode(str);//TR,@ALL,JY,OTHER
			optList.add(roleOpt);
		}

		boolean isSuccess = roleOptService.add(optList);
		System.out.println(isSuccess);
	}

	private void listPage() {
		Page<Role> page = roleService.listAll(new Page<Role>());
		System.out.println("总共有多少条数据" + page.getCount() + ",一共" + page.getTotalPage() + "分页，每页" + page.getPageSize()
				+ "条,当前在" + page.getCurrentPage() + "页");
		for (Role role : page.getList()) {
			System.out.println(role.getRoleCode() + " " + role.getRoleName());
		}
		// -------------------------
		page = roleService.listAll(new Page<Role>(2, 3));
		System.out.println("总共有多少条数据" + page.getCount() + ",一共" + page.getTotalPage() + "分页，每页" + page.getPageSize()
				+ "条,当前在" + page.getCurrentPage() + "页");
		for (Role role : page.getList()) {
			System.out.println(role.getRoleCode() + " " + role.getRoleName());
		}
		// -------------------------
		page = roleService.listAll(new Page<Role>(3, 3));
		System.out.println("总共有多少条数据" + page.getCount() + ",一共" + page.getTotalPage() + "分页，每页" + page.getPageSize()
				+ "条,当前在" + page.getCurrentPage() + "页");
		for (Role role : page.getList()) {
			System.out.println(role.getRoleCode() + " " + role.getRoleName());
		}
		// -------------------------
		page = roleService.listAll(new Page<Role>(4, 3));
		System.out.println("总共有多少条数据" + page.getCount() + ",一共" + page.getTotalPage() + "分页，每页" + page.getPageSize()
				+ "条,当前在" + page.getCurrentPage() + "页");
		for (Role role : page.getList()) {
			System.out.println(role.getRoleCode() + " " + role.getRoleName());
		}
	}
	private void listPageTwo() {
		Page page = roleService.listAll(new Page());
		System.out.println("总共有多少条数据" + page.getCount() + ",一共" + page.getTotalPage() + "分页，每页" + page.getPageSize()
				+ "条,当前在" + page.getCurrentPage() + "页");
		for (Object obj : page.getList()) {
			Role role = (Role)obj;
			System.out.println(role.getRoleCode() + " " + role.getRoleName());
		}
	}
}
